package com.meetflow.conference.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.meetflow.common.core.domain.entity.SysUser;
import com.meetflow.common.utils.SecurityUtils;
import com.meetflow.conference.domain.MeetRecord;
import com.meetflow.conference.service.IMeetRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.meetflow.common.annotation.Log;
import com.meetflow.common.core.controller.BaseController;
import com.meetflow.common.core.domain.AjaxResult;
import com.meetflow.common.enums.BusinessType;
import com.meetflow.conference.domain.MeetFlowAction;
import com.meetflow.conference.service.IMeetFlowActionService;
import com.meetflow.common.utils.poi.ExcelUtil;
import com.meetflow.common.core.page.TableDataInfo;

/**
 * 待办Controller
 *
 * @author ruoyi
 * @date 2024-07-15
 */
@RestController
@RequestMapping("/conference/flow")
public class MeetFlowActionController extends BaseController {
    @Autowired
    private IMeetFlowActionService meetFlowActionService;

    @Autowired
    private IMeetRecordService meetRecordService;


    /**
     * 查询待办列表
     */
    @PreAuthorize("@ss.hasPermi('conference:flow:list')")
    @GetMapping("/list")
    public TableDataInfo list(MeetRecord meetRecord) {
        if (null == meetRecord.getApproverUserId()) {
            meetRecord.setApproverUserId(SecurityUtils.getUserId());
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        meetRecord.setMeetSimFirstCode(user.getUserId());
        meetRecord.setUserLevel(user.getUserLevel());
        startPage();
        List<MeetRecord> list = meetRecordService.selectMeetRecordListByParam(meetRecord);
        return getDataTable(list);
    }

    @GetMapping(value = "/getToDoNum")
    public AjaxResult getToDoNum() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        return success(meetRecordService.getToDoNum(user.getUserId(), user.getUserLevel()));
    }


    @GetMapping(value = "/getAllRecordIds")
    public AjaxResult getAllRecordIds(MeetRecord meetRecord) {
        if (null == meetRecord.getApproverUserId()) {
            meetRecord.setApproverUserId(SecurityUtils.getUserId());
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        meetRecord.setUserLevel(user.getUserLevel());
        return success(meetRecordService.getAllRecordIds(meetRecord));
    }


    /**
     * 导出待办列表
     */
    @PreAuthorize("@ss.hasPermi('conference:flow:export')")
    @Log(title = "待办", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MeetFlowAction meetFlowAction) {
        List<MeetFlowAction> list = meetFlowActionService.selectMeetFlowActionList(meetFlowAction);
        ExcelUtil<MeetFlowAction> util = new ExcelUtil<MeetFlowAction>(MeetFlowAction.class);
        util.exportExcel(response, list, "待办数据");
    }

    /**
     * 获取待办详细信息
     */
    @PreAuthorize("@ss.hasPermi('conference:flow:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(meetRecordService.selectMeetRecordDetailById(id));
    }


    /**
     * 驳回
     */
    @Log(title = "驳回", businessType = BusinessType.UPDATE)
    @PostMapping("/rejectRecord")
    public AjaxResult rejectRecord(@RequestBody MeetRecord meetRecord) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        meetRecord.setMeetSimFirstCode(user.getUserId());
        meetRecord.setMeetSimFirstName(user.getNickName());
        return toAjax(meetRecordService.rejectMeetRecord(meetRecord));
    }

    /**
     * 更新
     */
    @Log(title = "更新", businessType = BusinessType.UPDATE)
    @PostMapping("/updateRecord")
    public AjaxResult updateRecord(@RequestBody MeetRecord meetRecord) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        meetRecord.setMeetSimFirstCode(user.getUserId());
        meetRecord.setMeetSimFirstName(user.getNickName());
        meetRecord.setAutoUpgradationTime(new Date());
        MeetFlowAction action = new MeetFlowAction();
        action.setMeetRecordId(meetRecord.getId());
        action.setMeetSimCode(String.valueOf(meetRecord.getMeetSimFirstCode()));
        action.setMeetSimName(meetRecord.getMeetSimFirstName());
        action.setMeetSimTime(new Date());
        action.setTaskStatus(meetRecord.getTaskStatus());
        action.setMeetStatus(5l);
        action.setMeetAction(meetRecord.getMeetAction());
        meetFlowActionService.insertMeetFlowAction(action);
        return toAjax(meetRecordService.acceptanceMeetRecord(meetRecord));
    }

    /**
     * 撤销
     */
    @Log(title = "撤销", businessType = BusinessType.UPDATE)
    @GetMapping("/cancelRecord/{id}")
    public AjaxResult cancelRecord(@PathVariable("id") Long id) {
        MeetRecord meetRecord = new MeetRecord();
        meetRecord.setId(id);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        meetRecord.setMeetSimFirstCode(user.getUserId());
        meetRecord.setMeetSimFirstName(user.getNickName());
        return toAjax(meetRecordService.closeMeetRecord(meetRecord));
    }

    /**
     * 受理
     */
    @Log(title = "受理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult submitRecord(@RequestBody MeetRecord meetRecord) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        meetRecord.setMeetSimFirstCode(user.getUserId());
        meetRecord.setMeetSimFirstName(user.getNickName());
        meetRecord.setAutoUpgradationTime(new Date());
        MeetFlowAction action = new MeetFlowAction();
        action.setMeetRecordId(meetRecord.getId());
        action.setMeetSimCode(String.valueOf(meetRecord.getMeetSimFirstCode()));
        action.setMeetSimName(meetRecord.getMeetSimFirstName());
        action.setMeetSimTime(new Date());
        action.setTaskStatus(meetRecord.getTaskStatus());
        action.setMeetAction(meetRecord.getMeetAction());
        String operationStatus = meetRecord.getOperationStatus();
        // 升级
        if (StringUtils.isNotEmpty(operationStatus) && "1".equals(operationStatus)) {
            if ("1".equals(user.getUserLevel()) && Integer.valueOf(meetRecord.getTaskStatus()) + 1 > 2) {
                return error("层级1用户只能升级问题至层级2");
            }
            if ("2".equals(user.getUserLevel()) && Integer.valueOf(meetRecord.getTaskStatus()) + 1 > 3) {
                return error("层级2用户只能升级问题至层级3");
            }
            if (StringUtils.isNotEmpty(meetRecord.getTaskStatus()) && Integer.valueOf(meetRecord.getTaskStatus()) + 1 <= 4){
                meetRecord.setTaskStatus(String.valueOf(Integer.valueOf(meetRecord.getTaskStatus()) + 1));
            } else {
                return error("当前层级已是最高级");
            }
            action.setMeetStatus(Long.valueOf(2));
        } else if (StringUtils.isNotEmpty(operationStatus) && "2".equals(operationStatus)) {// 驳回
            action.setMeetStatus(Long.valueOf(3));
            if (StringUtils.isNotEmpty(meetRecord.getTaskStatus()) && Integer.valueOf(meetRecord.getTaskStatus()) - 1 >0) {
                meetRecord.setTaskStatus(String.valueOf(Integer.valueOf(meetRecord.getTaskStatus()) - 1));
            }
        } else if (StringUtils.isNotEmpty(operationStatus) && "3".equals(operationStatus)) {// 委托
            action.setMeetStatus(Long.valueOf(6));
            String str = user.getNickName()+"委托"+ meetRecord.getApproverUserName()+"处理";
            if (StringUtils.isEmpty(action.getMeetAction())){
                action.setMeetAction(str);
            } else {
                action.setMeetAction(action.getMeetAction() + ";" + str);
            }
        } else if (StringUtils.isNotEmpty(operationStatus) && "4".equals(operationStatus)) {// 关闭
            action.setMeetStatus(Long.valueOf(4));
            meetRecord.setCheckStatus("2");
        } else {
            if (StringUtils.isNotEmpty(meetRecord.getCheckStatus()) && "2".equals(meetRecord.getCheckStatus())) {
                meetRecord.setPersonLiableNo(user.getUserId());
                meetRecord.setPersonLiableName(user.getNickName());
                if (Integer.valueOf(meetRecord.getTaskStatus()) <=2) {
                    meetRecord.setCheckStatus("4"); // 临时关闭
                    action.setMeetStatus(Long.valueOf(4));
                } else {
                    meetRecord.setCheckStatus("2");
                }

            } else if (StringUtils.isNotEmpty(meetRecord.getCheckStatus()) && "1".equals(meetRecord.getCheckStatus())) {
                action.setMeetStatus(Long.valueOf(1));
            } else if (StringUtils.isNotEmpty(meetRecord.getCheckStatus()) && "4".equals(meetRecord.getCheckStatus())) {
                action.setMeetStatus(Long.valueOf(4));
                meetRecord.setCheckStatus("2");
            } else if (StringUtils.isNotEmpty(meetRecord.getCheckStatus()) && "3".equals(meetRecord.getCheckStatus())) { //驳回
                action.setMeetStatus(Long.valueOf(3));
                meetRecord.setCheckStatus("1");
            }
        }

        meetFlowActionService.insertMeetFlowAction(action);
        return toAjax(meetRecordService.acceptanceMeetRecord(meetRecord));
    }

    /**
     * 删除待办
     */
    @PreAuthorize("@ss.hasPermi('conference:flow:remove')")
    @Log(title = "待办", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(meetFlowActionService.deleteMeetFlowActionByIds(ids));
    }
}
